package com.sirun.cn.fca.module.main.home.temp.control;

import android.app.Activity;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.sirun.cn.fca.R;
import com.sirun.cn.fca.module.main.home.temp.repo.ControlPollingResponse;
import com.sirun.cn.fca.module.main.home.temp.repo.ControlRequest;
import com.sirun.cn.fca.module.main.home.temp.repo.ControlResponse;
import com.sirun.cn.fca.module.main.home.temp.repo.OperationStatusEnum;
import com.tencent.cloud.iov.flow.view.IContentViewContainer;
import com.tencent.cloud.iov.flow.view.impl.BaseFlowView;
import com.tencent.cloud.iov.kernel.model.BaseResponse;
import com.tencent.cloud.iov.util.storage.TXSharedPreferencesUtils;

import cn.sirun.com.fca.account.constant.CarConst;
import cn.sirun.com.fca.account.constant.ControlIntentConst;
import pl.droidsonroids.gif.GifImageView;

public class ControlFlowView extends BaseFlowView<ControlContract.Presenter>
        implements ControlContract.View {

    private TextView mStatusTV, mResultTV;
    private ImageView mStatusIV, mFlagIV;
    private GifImageView mStatusGIV;
    private int mControlType;

    public ControlFlowView(Activity activity) {
        super(activity);
    }

    public ControlFlowView(Activity activity, IContentViewContainer container) {
        super(activity, container);
    }


    @Override
    public void onCreate() {
        super.onCreate();
        initView();
    }

    private void initView() {
        setContentView(R.layout.control_activity);
        final Activity activity = getActivity();
        mStatusIV = activity.findViewById(R.id.iv_control_status);
        mStatusGIV = activity.findViewById(R.id.iv_control_gif);
        mStatusTV = activity.findViewById(R.id.tv_control_status);
        mResultTV = activity.findViewById(R.id.tv_control_complete);
        mFlagIV = activity.findViewById(R.id.iv_control_flag);
        mStatusGIV.post(new Runnable() {
            @Override
            public void run() {
                LinearLayout.LayoutParams linearParams = (LinearLayout.LayoutParams) mStatusIV.getLayoutParams();
                int statusIVWidth = mStatusGIV.getWidth();
                linearParams.width = statusIVWidth;
                linearParams.height = statusIVWidth;
                mStatusIV.setLayoutParams(linearParams);
                mStatusGIV.setLayoutParams(linearParams);
            }
        });
        mStatusGIV.setBackgroundResource(R.drawable.ic_control_polling);
        mControlType = getActivity().getIntent().getIntExtra(ControlIntentConst.CONTROL_TYPE, 0);
        mStatusTV.setText("控制中.....");
        sendControlRequest();
    }

    private void sendControlRequest() {
        String type = "";
        ControlRequest request = new ControlRequest();
        if (mControlType == ControlIntentConst.LOCK) {
            request.setState("LOCKED");
            type = "doors";
        } else if (mControlType == ControlIntentConst.UNLOCK) {
            request.setState("UNLOCKED");
            type = "doors";
        } else if (mControlType == ControlIntentConst.ENGINE_ON) {
            request.setState("STARTED");
            type = "engine";
        } else if (mControlType == ControlIntentConst.ENGINE_OFF) {
            request.setState("STOPPED");
            type = "engine";
        } else if (mControlType == ControlIntentConst.BEER) {
            request.setState("ON");
            type = "alarm";
        }
        getPresenter().controlCar(type, TXSharedPreferencesUtils.getValue(CarConst.SpKey.CAR_ACTIVATE_INFO), request);
    }

    @Override
    public void onInitData(String initInfo) {

    }

    @Override
    protected ControlContract.Presenter createPresenter() {
        return new ControlPresenter();
    }

    private void updateControlView(String status) {
        mResultTV.setVisibility(View.VISIBLE);
        if (OperationStatusEnum.SUCCESS.name().equals(status)) {
            if (mControlType == ControlIntentConst.LOCK) {
                mStatusTV.setText("上锁成功");
                mResultTV.setText("车门上锁操作已完成");
            } else if (mControlType == ControlIntentConst.UNLOCK) {
                mStatusTV.setText("解锁成功");
                mResultTV.setText("车门解锁操作已完成");
            } else if (mControlType == ControlIntentConst.ENGINE_ON) {
                mStatusTV.setText("启动引擎成功");
                mResultTV.setText("启动引擎操作已完成");
            } else if (mControlType == ControlIntentConst.ENGINE_OFF) {
                mStatusTV.setText("关闭引擎成功");
                mResultTV.setText("关闭引擎操作已完成");
            } else if (mControlType == ControlIntentConst.BEER) {
                mStatusTV.setText("鸣笛闪灯成功");
                mResultTV.setText("鸣笛闪灯操作已完成");
            }
            mStatusGIV.setVisibility(View.GONE);
            mStatusIV.setVisibility(View.VISIBLE);
            mStatusIV.setBackgroundResource(R.drawable.ic_control_success);
            mFlagIV.setVisibility(View.VISIBLE);
            mFlagIV.setBackgroundResource(R.drawable.ic_control_success_flag);
        } else {
            if (mControlType == ControlIntentConst.LOCK) {
                mStatusTV.setText("上锁失败");
                mResultTV.setText("车门上锁操作已完成");
            } else if (mControlType == ControlIntentConst.UNLOCK) {
                mStatusTV.setText("解锁失败");
                mResultTV.setText("车门解锁操作已完成");
            } else if (mControlType == ControlIntentConst.ENGINE_ON) {
                mStatusTV.setText("启动引擎失败");
                mResultTV.setText("启动引擎操作已完成");
            } else if (mControlType == ControlIntentConst.ENGINE_OFF) {
                mStatusTV.setText("关闭引擎失败");
                mResultTV.setText("关闭引擎操作已完成");
            } else if (mControlType == ControlIntentConst.BEER) {
                mStatusTV.setText("鸣笛闪灯失败");
                mResultTV.setText("鸣笛闪灯操作已完成");
            }
            mStatusGIV.setVisibility(View.GONE);
            mStatusIV.setVisibility(View.VISIBLE);
            mStatusIV.setBackgroundResource(R.drawable.ic_control_error);
            mFlagIV.setVisibility(View.VISIBLE);
            mFlagIV.setBackgroundResource(R.drawable.ic_control_error_flag);
        }
    }

    @Override
    public void onControlDoorResponse(BaseResponse<ControlResponse> response) {
        if (response.getCode() == 0) {
            if (response.getData().getStatus().equals(OperationStatusEnum.ONGOING.name())) {
                getPresenter().controlPolling("4", response.getData().getRequestId());
            } else if (response.getData().getStatus().equals(OperationStatusEnum.SUCCESS.name())) {
                updateControlView(OperationStatusEnum.SUCCESS.name());
            } else {
                updateControlView(OperationStatusEnum.ERROR.name());
            }
        } else {
            updateControlView(OperationStatusEnum.ERROR.name());
        }
    }

    @Override
    public void onControlEngineResponse(BaseResponse<ControlResponse> response) {
        if (response.getCode() == 0) {
            if (response.getData().getStatus().equals(OperationStatusEnum.ONGOING.name())) {
                getPresenter().controlPolling("4", response.getData().getRequestId());
            } else if (response.getData().getStatus().equals(OperationStatusEnum.SUCCESS.name())) {
                updateControlView(OperationStatusEnum.SUCCESS.name());
            } else {
                updateControlView(OperationStatusEnum.ERROR.name());
            }
        } else {
            updateControlView(OperationStatusEnum.ERROR.name());
        }
    }

    @Override
    public void onControlBeerResponse(BaseResponse<ControlResponse> response) {
        if (response.getCode() == 0) {
            if (response.getData().getStatus().equals(OperationStatusEnum.ONGOING.name())) {
                getPresenter().controlPolling("4", response.getData().getRequestId());
            } else if (response.getData().getStatus().equals(OperationStatusEnum.SUCCESS.name())) {
                updateControlView(OperationStatusEnum.SUCCESS.name());
            } else {
                updateControlView(OperationStatusEnum.ERROR.name());
            }
        } else {
            updateControlView(OperationStatusEnum.ERROR.name());
        }
    }

    @Override
    public void onControlPollingRespons(BaseResponse<ControlPollingResponse> response) {
        if (response.getCode() == 0) {
            if (response.getData().getStatus().equals(OperationStatusEnum.ONGOING.name())) {
                getPresenter().controlPolling("4", response.getData().getRequestId());
            } else if (response.getData().getStatus().equals(OperationStatusEnum.SUCCESS.name())) {
                updateControlView(OperationStatusEnum.SUCCESS.name());
            } else {
                updateControlView(OperationStatusEnum.ERROR.name());
            }
        } else {
            updateControlView(OperationStatusEnum.ERROR.name());
        }
    }

    @Override
    public void onControlCarResponse(BaseResponse<ControlResponse> response) {
        if (response.getCode() == 0) {
            if (response.getData().getStatus().equals(OperationStatusEnum.ONGOING.name())) {
                getPresenter().controlPolling("4", response.getData().getRequestId());
            } else if (response.getData().getStatus().equals(OperationStatusEnum.SUCCESS.name())) {
                updateControlView(OperationStatusEnum.SUCCESS.name());
            } else {
                updateControlView(OperationStatusEnum.ERROR.name());
            }
        } else {
            updateControlView(OperationStatusEnum.ERROR.name());
        }
    }

}
